package com.lwm.service.impl;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwm.domain.ProjectMaintain;
import com.lwm.domain.ResponseResult;
import com.lwm.mapper.ProjectMaintainMapper;
import com.lwm.service.ProjectMaintainService;
import com.lwm.utils.Code;
import com.lwm.utils.HintMessage;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 项目维护业务层
 */
@Service
public class ProjectMaintainServiceImpl implements ProjectMaintainService {
    @Resource
    private ProjectMaintainMapper projectMaintainMapper;

    /**
     * 保存
     *
     * @param projectMaintain 项目维护实体类
     * @return {@link ResponseResult}<{@link Object}>
     */
    @Override
    public ResponseResult<Object> save(ProjectMaintain projectMaintain) {
        // 必填参数 项目名、负责人、开始日期、结束日期、项目状态
        if (!StringUtils.hasText(projectMaintain.getProjectName())
                || !StringUtils.hasText(projectMaintain.getPrincipal())
                || projectMaintain.getStartDate() == null
                || projectMaintain.getEndDate() == null
                || projectMaintain.getProjectStatus() == null
        ) {
            return new ResponseResult<>(Code.SAVE_ERR, HintMessage.MUST_PARAM_HINT);
        }
        projectMaintain.setId(IdUtil.getSnowflakeNextId());//设置id
        projectMaintainMapper.insert(projectMaintain);
        return new ResponseResult<>(Code.SAVE_OK, HintMessage.SAVE_OK_HINT);
    }

    /**
     * 删除
     *
     * @param ids id
     * @return {@link ResponseResult}<{@link Object}>
     */
    @Override
    public ResponseResult<Object> remove(List<Long> ids) {
        if (ids.isEmpty()) {
            return new ResponseResult<>(Code.DELETE_ERR, HintMessage.REMOVE_LACK_ID + "项目");
        }
        int i = projectMaintainMapper.delete(ids);
        if (i <= 0) return new ResponseResult<>(Code.DELETE_ERR, HintMessage.REMOVE_ERR_HINT);
        return new ResponseResult<>(Code.DELETE_OK, HintMessage.REMOVE_OK_HINT);
    }

    /**
     * 编辑
     *
     * @param projectMaintain 项目维护实体类
     * @return {@link ResponseResult}<{@link Object}>
     */
    @Override
    public ResponseResult<Object> edit(ProjectMaintain projectMaintain) {
        if (null == projectMaintain.getId()) {
            return new ResponseResult<>(Code.UPDATE_ERR, HintMessage.EDIT_LACK_ID + "项目");
        }
        int i = projectMaintainMapper.update(projectMaintain);
        if (i <= 0) return new ResponseResult<>(Code.UPDATE_ERR, HintMessage.EDIT_ERR_HINT);
        return new ResponseResult<>(Code.UPDATE_OK, HintMessage.EDIT_OK_HINT);
    }

    /**
     * 分页
     *
     * @param page            页码
     * @param size            数量
     * @param projectMaintain 项目维护实体类
     * @return {@link ResponseResult}<{@link PageInfo}<{@link ProjectMaintain}>>
     */
    @Override
    public ResponseResult<PageInfo<ProjectMaintain>> page(Integer page, Integer size, ProjectMaintain projectMaintain) {
        if (page == null) page = 1;
        if (size == null) size = 5;
        PageInfo<ProjectMaintain> pageInfo = PageHelper
                .startPage(page, size)
                .doSelectPageInfo(() -> projectMaintainMapper.selectAll(projectMaintain));
        return new ResponseResult<>(Code.GET_OK, HintMessage.GET_OK_HINT, pageInfo);
    }

    /**
     * 根据id获取
     *
     * @param id 项目id
     * @return {@link ResponseResult}<{@link Object}>
     */
    @Override
    public ResponseResult<Object> getById(Long id) {
        if (null == id) return new ResponseResult<>(Code.GET_ERR, HintMessage.GET_LACK_ID + "项目");
        ProjectMaintain projectMaintain = projectMaintainMapper.selectById(id);
        if (Objects.isNull(projectMaintain)) return new ResponseResult<>(Code.GET_ERR, HintMessage.GET_ERR_HINT);
        return new ResponseResult<>(Code.GET_OK, HintMessage.GET_OK_HINT, projectMaintain);
    }
}
